﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataLayer;
using DataLayer.Model.Classes;

namespace Overview
{
    /// <summary>
    /// Interaction logic for ClassSelector.xaml
    /// </summary>
    public partial class ClassSelector : Window
    {
        public Type Type { get; private set; }

        private const int OPTION_CONTROL_WIDTH = 128;
        private const int OPTION_CONTROL_HEIGHT = 190;
       // public Character Character;
        public ClassSelector()
        {
            InitializeComponent();
            Type = typeof(Character);
            FillGridWithClassOptions();
        }
        public ClassSelector(Type pType)
        {
            InitializeComponent();
            Type = pType;
            FillGridWithClassOptions();
        }

        private void FillGridWithClassOptions()
        {
            var pClasses = Singleton.GetChildClasses(Type);
            this.Width = OPTION_CONTROL_WIDTH * pClasses.Count;
            this.Height = OPTION_CONTROL_HEIGHT;
            foreach (var charClass in pClasses)
            {
                int gridColumnId = AddColumnToGrid(ClassChangeOptions);
                var classOptionControll = InitClassOption(charClass, Singleton.GetChildClasses(charClass).Count > 0);
                Grid.SetColumn(classOptionControll, gridColumnId);
                Grid.SetRow(classOptionControll, 0);
                ClassChangeOptions.Children.Add(classOptionControll);
            }
        }

        private int AddColumnToGrid(Grid pGrid)
        {
            pGrid.ColumnDefinitions.Add(new ColumnDefinition());
            //TODO find way to ass id directly from row defenition 
            return pGrid.ColumnDefinitions.Count - 1;
        }

        private CharClassOption InitClassOption(Type charClass, bool pNotLastClass)
        {
            var classOption = new CharClassOption(charClass,pNotLastClass);
              //  classOption.SetChangeClasses(Singleton.GetChildClasses(charClass));
            classOption.InitializeComponent();
            classOption.OnOcupationSelected += btnSelectClasses_Click;
            classOption.OnNextStepSelected += btnNextClasses_Click;
            return classOption;
        }

        public Character DefineCharacterClass()
        {
            return null;
        }

        private void btnNextClasses_Click(object sender, EventArgs e)
        {
            Type = (Type)sender;
            var classSelector = new ClassSelector(Type);
            if (classSelector.ShowDialog() == true)
            {
                Type = classSelector.Type;
                btnSelectClasses_Click(Type, null);
            }
            else
            {
                CancelButton_Click(null,null);
            }
        }
        private void btnSelectClasses_Click(object sender, EventArgs e)
        {
            Type = (Type) sender;
            DialogResult = true;
            Close();
        }
        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }

    }
}
